//
//  VersionUpdateViewController.m
//  EMMApp
//
//  Created by Mac on 16/6/29.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "VersionUpdateViewController.h"

/** Api
 *
 *  Api调用导入.h文件
 */
#import "GetVersionApi.h"

/** Cell
 *
 *  Cell导入.h文件
 */
#import "MarketingCell.h"
#import "versionCell.h"


@interface VersionUpdateViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate> {
    UITextView *inputTextView;
}
/**
 创建UITableView
 */
@property (nonatomic, strong) UITableView *examineListTableView;

/**
 GetVersion - model
 */
@property (nonatomic, strong) GetVersion *getVersion;

@end

@implementation VersionUpdateViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self postGetCalculatePriceApi];
    [self creatNavigationController];
    self.view.backgroundColor = [UIColor whiteColor];
    _examineListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _examineListTableView.delegate = self;
    _examineListTableView.dataSource = self;
    _examineListTableView.backgroundColor = [UIColor colorFromHexCode:@"#f7f7f7"];
    _examineListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_examineListTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 版本说明请求Api
- (void)postGetCalculatePriceApi {
    GetVersionApi *api = [[GetVersionApi alloc] initWith];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetVersionApi *result = (GetVersionApi *)request;
        if (result.isSuccess) {
            GetVersion *get = result.resultData;
            _getVersion = get;
        } else {
            [self showToast:result.resultData.errorMsg];
        }
        [_examineListTableView reloadData];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"版本说明";
    [self configLeftBtn];
}

#pragma mark - UITableView 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"versionCell";
    versionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"versionCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorFromHexCode:@"#f7f7f7"];
    cell.oldName.text = [NSString stringWithFormat:@"当前版本 %@",apiVString];
    if (indexPath.row == 1) {
        static NSString *CellIdentifier = @"MarketingCell";
        MarketingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MarketingCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        inputTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, EMM_WIDTH-20, 170)];
        inputTextView.editable = NO;
        inputTextView.delegate = self;
        inputTextView.keyboardType = UIKeyboardTypeDefault;
        inputTextView.returnKeyType = UIReturnKeyDone;
        inputTextView.font = [UIFont systemFontOfSize:16];
        [cell addSubview:inputTextView];
        if ([_getVersion.versionDes isEqualToString:@"暂无版本更新!"] || [_getVersion.versionDes isEqualToString:@"您已是最新版本,无需更新!"]) {
            inputTextView.text = @" 1.修复线上bug";
        }
        return cell;
    }
    return cell;
}

#pragma mark - UITextView代理
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (textView == inputTextView) {
        return NO;
    }
    return NO;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    //    UITextView *text=(UITextView *)sender;
    [UIMenuController sharedMenuController].menuVisible = NO;  //donot display the menu
    [inputTextView resignFirstResponder];                     //do not allow the user to selected anything
    return NO;
}



@end
